package cn.lisens.modules.cms.dao;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;


import cn.lisens.common.dao.AbstractBaseDao;
import cn.lisens.modules.cms.model.Customer;

@Repository
public class CustomerDaoImpl extends AbstractBaseDao<Customer, Integer> implements CustomerDao {

	@Autowired
	private SessionFactory sessionFactory;
//	@Autowired
//	private NamedParameterJdbcTemplate namedTemplate;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public List<Customer> getCustomersHql() {
		Session session = sessionFactory.getCurrentSession();
		//1.hql,customer是实体类的对象
//		String sql = "from customer where id = :id";
//		Query<Customer> q = session.createQuery(sql,Customer.class);
//		q.setParameter("id", 2);
//		return q.getResultList();
		//2.criteria方式
		CriteriaBuilder builder = session.getCriteriaBuilder();
		CriteriaQuery<Customer> cq = builder.createQuery(Customer.class);
		Root<Customer> root = cq.from(Customer.class);
		cq.select(root);
		cq.where(builder.equal(root.get("id"), 2));
		Query<Customer> query = session.createQuery(cq);
		return query.getResultList();
	}
	
	@Override
	public List<Customer> getCustomers() {
		//原生sql
		String sql = "select * from customer ";
//		Customer customer = new Customer();
//		customer.setFirstName("张三");
//		SqlParameterSource param = new BeanPropertySqlParameterSource(customer);
//		//1.使用名字作为查询条件
//		return namedTemplate.query(sql, param, new BeanPropertyRowMapper<Customer>(Customer.class));
		//2.使用jdbc作为查询条件
		return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Customer>(Customer.class));
		//3.jdbc自定义返回列
//		List<Customer> list = jdbcTemplate.query(sql, new RowMapper<Customer>() {
//			@Override
//			public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
//				Customer customer = new Customer();
//				customer.setId(rs.getInt("id"));
//				customer.setFirstName(rs.getString("first_name"));
//				customer.setLastName(rs.getString("last_Name"));
//				customer.setEmail(rs.getString("email"));
//				return customer;
//			}
//		});
//		return list;
	}

	@Override
	public void saveCustomer(Customer theCustomer) {
		Session currentSession = sessionFactory.getCurrentSession();
	    currentSession.saveOrUpdate(theCustomer);
	}

	@Override
	public Customer getCustomer(int theId) {
		Session currentSession = sessionFactory.getCurrentSession();
        Customer theCustomer = currentSession.get(Customer.class, theId);
        return theCustomer;
	}

	@Override
	public void deleteCustomer(int theId) {
		Session session = sessionFactory.getCurrentSession();
	    Customer book = session.byId(Customer.class).load(new Integer(theId));
	    session.delete(book);

	}

}
